import React from "react";
import {Card, message, Modal} from 'antd';
import {PermissApi} from '@/api/index'
import TreeWithMenu from './components/treeWithMenu'
import Add from './components/add'
import Update from './components/update'
import {ExclamationCircleOutlined} from "@ant-design/icons";

export default class Index extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            addModalVisible: false,
            updateModalVisible: false,
            parentId:-1,
            parentName:'',
            id:null,
            list: []
        };
    }

    async getPermissList() {
        let ret = await PermissApi.list();
        if (ret && ret.code === 200) {
            this.setState({
                list: ret.data || []
            })
        }
    }

    async delPermiss(id) {
        let ret = await PermissApi.del({id: id});
        if (ret && ret.code === 200) {
            message.success('删除权限成功！');
            this.getPermissList();
        } else {
            message.error(ret.message)
        }
    }

    componentDidMount() {
        this.getPermissList()
    }

    // 新建权限
    add(item) {
        let {key, title} = item.props
        this.setState({
            addModalVisible: true,
            parentId: key,
            parentName: title
        })
    }

    addModalClose() {
        this.setState({
            addModalVisible: false
        })
        this.getPermissList()
    }

    // 删除b
    async del(item) {
        let that = this
        Modal.confirm({
            title: '确认删除权限?',
            icon: <ExclamationCircleOutlined/>,
            content: '删除父权限同时删除其下子权限',
            okText: '是',
            cancelText: '否',
            onOk() {
                that.delPermiss(item.props.key)
            }
        })
    }

    // 修改权限
    update(item) {
        this.setState({
            updateModalVisible: true,
            id: item.props.key
        })
    }

    updateModalClose() {
        this.setState({
            updateModalVisible: false,
        })
        this.getPermissList()
    }

    render() {

        const menus = {
            menuId: 1,
            items: [
                {name: '添加', handler: this.add.bind(this)},
                {name: '删除', handler: this.del.bind(this)},
                {name: '修改', handler: this.update.bind(this)},
            ]
        }

        const {parentId, parentName, addModalVisible, updateModalVisible, list, id} = this.state

        return (
            <Card title="权限管理">
                <TreeWithMenu dataSource={list} contextMenu={menus}></TreeWithMenu>
                <Add modalVisible={addModalVisible}
                     onClose={this.addModalClose.bind(this)}
                     parentId={parentId}
                     parentName={parentName}
                />
                <Update modalVisible={updateModalVisible}
                        onClose={this.updateModalClose.bind(this)}
                        id={id}
                />
            </Card>
        );
    }
}

Index.defaultProps = {}
